/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utn.tsb.tej.front_end;

import com.trolltech.qt.core.QSize;
import com.trolltech.qt.core.Qt.AlignmentFlag;
import utn.tsb.tej.front_end.Maps.Map;
import com.trolltech.qt.core.Qt.Orientation;
import com.trolltech.qt.core.Qt.TextElideMode;
import com.trolltech.qt.core.Qt.TransformationMode;
import com.trolltech.qt.gui.*;
import java.io.File;
import utn.tsb.tej.comunications.Preferences;


/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class MainWindow extends QFrame {

    public MainWindow() {
        this.setWindowTitle("T.E.J. - Under Development Version");
        this.showMaximized();
        QIcon icoTEJ = new QIcon(getClass().getResource("/images/tej_icon.png").getPath());
        this.setWindowIcon(icoTEJ);
        Preferences.loadPreferencesFromFile("file.json");

        /*Thread t = new Thread(new Client("192.168.0.104"));
        t.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        t.stop();*/

        QHBoxLayout main_layout = new QHBoxLayout(this);

        /*
         * Left Info Panels
         */
        QVBoxLayout vbox_LeftPanel = new QVBoxLayout();

        //Situation
        QHBoxLayout boxSituation = new QHBoxLayout();
        boxSituation.setSpacing(0);
        boxSituation.addWidget(new QLabel("Situation: "));
        QLabel lbl_Situation = new QLabel("Fronteras Cerradas");
        boxSituation.addWidget(lbl_Situation, 0, AlignmentFlag.AlignLeft);
        vbox_LeftPanel.addLayout(boxSituation);

        // First Tab Area (Left Panel)
        QTabWidget tabArea1 = new QTabWidget(this);
        tabArea1.setTabPosition(QTabWidget.TabPosition.West);
        tabArea1.setElideMode(TextElideMode.ElideRight);
        vbox_LeftPanel.addWidget(tabArea1);
        //Cards Table
        QTableWidget tableCards = new QTableWidget(5, 4);
        tableCards.horizontalHeader().setResizeMode(QHeaderView.ResizeMode.ResizeToContents);
        tableCards.verticalHeader().setResizeMode(QHeaderView.ResizeMode.ResizeToContents);
        tableCards.horizontalHeader().setStretchLastSection(true);
        tableCards.horizontalHeader().hide();
        tableCards.verticalHeader().hide();
        tabArea1.addTab(tableCards, "Cards");

        //this.loadTableImages(tableCards);

        //Second Tab Area (Left Panel)
        QTabWidget tabArea2 = new QTabWidget(this);
        tabArea2.setElideMode(TextElideMode.ElideRight);
        vbox_LeftPanel.addWidget(tabArea2);
        //Objectives Panel
        QListWidget lst_Objectives = new QListWidget(this);
        lst_Objectives.addItem("Objective1");
        tabArea2.addTab(lst_Objectives, "Objectives:");
        //Players Panel
        QListWidget lst_Players = new QListWidget(this);
        lst_Players.addItem("Player 1");
        lst_Players.addItem("Player 2");
        lst_Players.addItem("Player 3");
        tabArea2.addTab(lst_Players, "Players:");
        //Chat
        ChatWidget chatWidget = new ChatWidget(this);
        tabArea2.addTab(chatWidget, "Chat:");

        main_layout.addLayout(vbox_LeftPanel);

        /*
         * Right Panel. Here is where the map and the toolbar will be shown.
         */
        QVBoxLayout rightLayout = new QVBoxLayout();


        /*
         * ToolBar Panel. Here is where the toolbar is gonna be located.
         */
        QToolBar toolBar = new QToolBar(this);
        toolBar.setBaseSize(500, 64);
        toolBar.setMovable(false);
        toolBar.setOrientation(Orientation.Horizontal);
        toolBar.setIconSize(new QSize(64, 64));
        toolBar.setStyleSheet("QToolBar{ border: none; height: 64px;}");

        QIcon icoNewRG = new QIcon(getClass().getResource("/images/new_roomgame_64.png").getPath());
        QIcon icoJoinRG = new QIcon(getClass().getResource("/images/join_roomgame_64.png").getPath());
        QIcon icoPref = new QIcon(getClass().getResource("/images/preferences_64.png").getPath());
        QIcon icoHelp = new QIcon(getClass().getResource("/images/help_64.png").getPath());

        QAction actNewRG = toolBar.addAction(icoNewRG, "New RG");
        QAction actJoinRG = toolBar.addAction("Join RG");
        actJoinRG.setIcon(icoJoinRG);
        toolBar.addSeparator();
        //toolBar.addAction("Options");
        QAction actPreferences = toolBar.addAction(icoPref, "Preferences");
        toolBar.addSeparator();
        QAction actHelp = toolBar.addAction(icoHelp, "Help");

        actNewRG.triggered.connect(this, "actNewRoomGame_Click()");
        actJoinRG.triggered.connect(this, "actJoinRoomGame_Click()");
        actPreferences.triggered.connect(this, "actPreferences_Click()");
        actHelp.triggered.connect(this, "actHelp_Click()");

        /*
         * Map Panel. Here is where the map is gonna be located.
         */
        Map map = new Map(this);

        rightLayout.addWidget(toolBar);
        rightLayout.addWidget(map);

        main_layout.addLayout(rightLayout);        
    }

    private void loadTableImages(QTableWidget table){
        String osSep = File.separator;
        File dir = new File(getClass().getResource(osSep + "images" + osSep + "ChainChomp.png").getPath());
        File[] files = new File(dir.getParent()).listFiles();
        int x = -1;
        int y = 0;
        for(File f : files){
            if(x > 5) break;
            if(y > 4) y = 0;
            if(y == 0) x++;
            QLabel lblIcon = new QLabel();
            QPixmap pixmap = new QPixmap( f.getPath() );
            pixmap = pixmap.scaledToHeight(100, TransformationMode.FastTransformation);
            lblIcon.setPixmap(pixmap);
            table.setCellWidget(x, y, lblIcon);
            y++;
        }
    }

    public static void main(String[] args) {
        QApplication.initialize(args);

        MainWindow app = new MainWindow();
        app.show();
        QApplication.exec();
    }

    public void actNewRoomGame_Click()
    {
        NewRoomGameWindow nrgWindow = new NewRoomGameWindow();
        nrgWindow.setModal(true);
        nrgWindow.show();
    }

    public void actJoinRoomGame_Click()
    {
        JoinGameWindow jrgWindow = new JoinGameWindow();
        jrgWindow.setModal(true);
        jrgWindow.show();
    }

    public void actPreferences_Click()
    {
        PreferencesWindow prefWindow = new PreferencesWindow();
        prefWindow.setModal(true);
        prefWindow.show();
    }

    public void actHelp_Click()
    {
        WaitingWindow wait = new WaitingWindow();
        wait.show();
    }



}